package com.cn.ant.modules.wechat.web;

import com.cn.ant.common.utils.AjaxJson;
import com.cn.ant.common.utils.Identities;
import com.cn.ant.common.utils.StringUtils;
import com.cn.ant.common.web.BaseController;
import com.cn.ant.modules.sys.entity.User;
import com.cn.ant.modules.sys.utils.UserUtils;
import com.cn.ant.modules.wechat.entity.HuodongEntity;
import com.cn.ant.modules.wechat.entity.PrizeEntity;
import com.cn.ant.modules.wechat.entity.WeixinAccount;
import com.cn.ant.modules.wechat.service.HuodongEntityService;
import com.cn.ant.modules.wechat.service.WeixinAccountServiceImpl;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 微信活动Controller
 * @author AntDream
 * @version 2015-03-29
 */
@Controller
@RequestMapping(value = "${adminPath}/wechat/huodongEntity")
public class HuodongEntityController extends BaseController {

	@Autowired
	private HuodongEntityService huodongEntityService;
    @Autowired
    private WeixinAccountServiceImpl accountService;

	@ModelAttribute
	public HuodongEntity get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return huodongEntityService.get(id);
		}else{
			return new HuodongEntity();
		}
	}
	
	@RequiresPermissions("wechat:huodongEntity:view")
	@RequestMapping(value = {"list", ""})
	public String list(HuodongEntity huodongEntity, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
        Map<String,Object> param = new HashMap<String,Object>();
        List<HuodongEntity> list = huodongEntityService.find(param);
        model.addAttribute("list", list);
		return "modules/wechat/huodongEntityList";
	}

	@RequiresPermissions("wechat:huodongEntity:view")
	@RequestMapping(value = "form")
	public String form(HuodongEntity huodongEntity, Model model) {
		model.addAttribute("huodongEntity", huodongEntity);
        List<WeixinAccount> accountList =  accountService.findByLoginUser();
        model.addAttribute("accountList", accountList);
        return "modules/wechat/huodongEntityForm";
	}

	@RequiresPermissions("wechat:huodongEntity:edit")
	@RequestMapping(value = "save")
	@ResponseBody
	public AjaxJson save(HuodongEntity huodongEntity, Model model) {
        AjaxJson jo = new AjaxJson();
		try {
			if (!beanValidator(model, huodongEntity)) {
                jo.setSuccess(false);
                jo.setMsg("数据校验失败,请重试!");
			} else {
				if (StringUtils.isBlank(huodongEntity.getId())) {
					huodongEntity.setId(Identities.generateUUID());
                    List<PrizeEntity> prizeList = huodongEntity.getPrizeList();
                    for (int i = 0; i < prizeList.size(); i++) {
                        PrizeEntity prize = prizeList.get(i);
                        prize.setId(Identities.generateUUID());
                        prize.setUsenum(0);
                        prize.setHid(huodongEntity.getId());
                    }
                    huodongEntityService.save(huodongEntity);
                    huodongEntityService.batchAdd(prizeList);
                } else {
                    List<PrizeEntity> prizeList = huodongEntity.getPrizeList();
                    for (int i = 0; i < prizeList.size(); i++) {
                        PrizeEntity prize = prizeList.get(i);
                        prize.setId(Identities.generateUUID());
                        prize.setUsenum(0);
                        prize.setHid(huodongEntity.getId());
                    }
                    huodongEntityService.update(huodongEntity);
                    huodongEntityService.batchAdd(prizeList);
                }
            }
			msg = "保存微信活动'" + huodongEntity.getTitle() + "'成功";
            jo.setMsg(msg);
		} catch (Exception e) {
			e.printStackTrace();
            jo.setSuccess(false);
            jo.setMsg("操作失败,请联系管理员");
		}
		return jo;
	}
	
	@RequiresPermissions("wechat:huodongEntity:edit")
	@RequestMapping(value = "delete")
    @ResponseBody
	public AjaxJson delete(String id, RedirectAttributes redirectAttributes) {
        AjaxJson jo = new AjaxJson();
        if (StringUtils.isNotBlank(id)) {
            huodongEntityService.delete(id);
            jo.setMsg("删除成功,页面在2s后跳转..");
        } else {
            jo.setSuccess(false);
            jo.setMsg("参数错误,删除失败");
        }
        return jo;
	}

}
